package com.ellricky.baselib.widget.calendar;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static int selectPosition = -1;

    public static int getSelectPosition() {
        return selectPosition;
    }


    /**
     * 获取当前日期一周的日期
     *
     * @param date
     * @return
     */
    public static ArrayList<WeekDateEntity> getWeek(String date) {
        ArrayList<WeekDateEntity> result = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(dateFormat.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); //获取本周一的日期

        for (int i = 0; i < 7; i++) {
            WeekDateEntity entity = new WeekDateEntity();
            entity.setDate(
                    getValue(cal.get(Calendar.YEAR)) + "-" + getValue(cal.get(Calendar.MONTH) + 1) + "-" + getValue(cal.get(Calendar.DATE))
            );
            entity.setMillion(cal.getTimeInMillis());
            entity.setDay(getValue(cal.get(Calendar.DATE)));
            entity.setWeekNum(cal.get(Calendar.DAY_OF_WEEK));
            entity.setWeekName(getWeekName(entity.getWeekNum()));
            entity.setToday(isToday(entity.getDate()));
            cal.add(Calendar.DATE, 1);
            result.add(entity);
        }

        return result;

    }

    /**
     * 获取当前日期一月的日期
     *
     * @param date
     * @return
     */
//    public static ArrayList<WeekDateEntity> getMonth(String date) {
//        ArrayList<WeekDateEntity> result = new ArrayList<>();
//        Calendar cal = Calendar.getInstance();
//        try {
//            cal.setTime(new SimpleDateFormat("yyyy-MM").parse(date));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//        for (int i = 1; i <= max; i++) {
//            WeekDateEntity entity = new WeekDateEntity();
//            entity.setDate(getValue(
//                    cal.get(Calendar.YEAR)) + "-" + getValue(cal.get(Calendar.MONTH) + 1) + "-" + getValue(cal.get(Calendar.DATE)
//            ));
//            entity.setMillion(cal.getTimeInMillis());
//            entity.setWeekNum(cal.get(Calendar.DAY_OF_WEEK));
//            entity.setDay(getValue(cal.get(Calendar.DATE)));
//            entity.setWeekName(getWeekName(entity.getWeekNum()));
//            entity.setToday(isToday(entity.getDate()));
//            entity.setLuna(getLuna(entity.getDate()));
//            cal.add(Calendar.DATE, 1);
//            result.add(entity);
//        }
//        //为了用空的值填补第一个之前的日期
//        //先获取在本周内是周几
//        int weekNum = result.get(0).getWeekNum() - 1;
//        for (int j = 0; j < weekNum; j++) {
//            WeekDateEntity entity = new WeekDateEntity();
//            result.add(0, entity);
//        }
//        for (int i = 0; i < result.size(); i++) {
//            if (date.equals(result.get(i).getDate())) {
//                selectPosition = i;
//            }
//        }
//        return result;
//
//    }

    /**
     * 根据美式周末到周一 返回
     *
     * @param weekNum
     * @return
     */
    private static String getWeekName(int weekNum) {
        String name = "";
        switch (weekNum) {
            case 1:
                name = "星期日";
                break;
            case 2:
                name = "星期一";
                break;
            case 3:
                name = "星期二";
                break;
            case 4:
                name = "星期三";
                break;
            case 5:
                name = "星期四";
                break;
            case 6:
                name = "星期五";
                break;
            case 7:
                name = "星期六";
                break;
            default:
                break;
        }
        return name;
    }

    /**
     * 是否是今天
     *
     * @param sdate
     * @return
     */
    public static boolean isToday(String sdate) {
        boolean b = false;
        Date time = null;
        try {
            time = dateFormat.parse(sdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date today = new Date();
        if (time != null) {
            String nowDate = dateFormater.get().format(today);
            String timeDate = dateFormater.get().format(time);
            if (nowDate.equals(timeDate)) {
                b = true;
            }
        }
        return b;
    }

    /**
     * 个位数补0操作
     *
     * @param num
     * @return
     */
    public static String getValue(int num) {
        return String.valueOf(num > 9 ? num : ("0" + num));
    }


    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    /**
     * 获取系统当前日期
     */
    public static String getCurrDate(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    /**
     * 格式化日期
     */
    public static String formatDate(String date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date curDate = null;//获取当前时间
        try {
            curDate = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String str = formatter.format(curDate);
        return str;
    }

    /**
     * 切换周的时候用
     * 获取前/后 几天的一个日期
     *
     * @param currentData
     * @param dayNum
     * @return
     */
    public static String getSomeDays(String currentData, int dayNum) {
        Calendar c = Calendar.getInstance();
        //过去七天
        try {
            c.setTime(DateUtils.dateFormat.parse(currentData));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.add(Calendar.DATE, dayNum);
        Date d = c.getTime();
        String day = DateUtils.dateFormat.format(d);
        return day;
    }


    /**
     * 获取前/后 几个月的一个日期  切换月的时候用
     *
     * @param currentData
     * @param monthNum
     * @return
     */
//    public static String getSomeMonthDay(String currentData, int monthNum) {
//        Calendar c = Calendar.getInstance();
//        try {
//            c.setTime(new SimpleDateFormat("yyyy-MM").parse(currentData));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + monthNum);
//        Date day = c.getTime();
//        return new SimpleDateFormat("yyyy-MM-dd").format(day);
//    }

    /**
     * 获取阴历
     *
     * @param date
     * @return
     */
    public static String getLuna(String date) {
        Calendar today = Calendar.getInstance();
        try {
            today.setTime(Lunar.chineseDateFormat.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Lunar(today).toString();
    }
}
